package com.sudichina.sudichina.view.activity.setting;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.tv_setting_change_phonenumber)
    TextView tvSettingChangePhonenumber;
    @BindView(R.id.tv_setting_change_login_psd)
    TextView tvSettingChangeLoginPsd;
    @BindView(R.id.tv_setting_change_deal_psd)
    TextView tvSettingChangeDealPsd;
    @BindView(R.id.activity_setting)
    LinearLayout activitySetting;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        //检测是否登录
        LoginActivity.startMeAndInitIsLogin(this,Contains.SETTING_EXTRA);
        tvTitle.setText("设置");
        //TODO 左侧未完成
        tvRight.setVisibility(View.VISIBLE);
    }




    private void showGotoSetDealPsdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("设置交易密码");
        builder.setMessage("当前用户没有设置交易密码,是否立刻去设置?");
        builder.setNegativeButton("去设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(SettingActivity.this, SetDealPswActivity.class));
                alertDialog.dismiss();

            }
        });
        builder.setPositiveButton("稍后再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
        alertDialog = builder.create();
        alertDialog.show();

    }

    @OnClick({R.id.tv_setting_change_phonenumber, R.id.tv_setting_change_login_psd, R.id.tv_setting_change_deal_psd, R.id.iv_back})
    public void onClick(View view) {


        switch (view.getId()) {
            case R.id.tv_setting_change_phonenumber:
                startActivity(new Intent(this, ChangePhoneNumberActivity.class));
                break;
            case R.id.tv_setting_change_login_psd:
                startActivity(new Intent(this, ChangeLoginPswActivity.class));
                break;
            case R.id.tv_setting_change_deal_psd:
                //得到请求码 如果没有交易密码就去设置一个交易密码
                requestStatue();

                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    //"accountName":"15321859523"
    private void requestStatue() {


        HashMap<String, Object> map = new HashMap<>();
        map.put("accountName", MyApp.getCurrentUser().getAccountName());

        Type type = new TypeToken<Result>() {
        }.getType();
        NetUtils.requestData(Api.HAS_DEAL_PSW, map, this, type, false);


    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        Result bean1 = (Result) bean;
        int code = bean1.getCode();
        if (code == 8017) {
//无交易密码
            showGotoSetDealPsdDialog();
        } else if (code == 8016) {
            startActivity(new Intent(this, ChangeDealPswActivity.class));
        } else {
            startActivity(new Intent(this, LoginActivity.class));
        }

    }

    @Override
    public void requestdataFailed(IOException e) {

    }
}
